var myDatepicker = myApp.directive('myDatepicker', ['$rootScope', '$parse', '$filter', function ($rootScope, $parse, $filter) {
    return {
        restrict: 'E',
        require: '?ngModel',
        scope: true,
        link: function ($scope, elem, attrs, ctrl) {
            if (attrs.minDate) {
                // $(elem).find('.input-group').attr('data-date-start-date', attrs.minDate);
                $(elem).find('.myDate').attr('data-date-start-date', attrs.minDate);
            }
            if (attrs.maxDate) {
                // $(elem).find('.input-group').attr('data-date-end-date', attrs.maxDate);
                $(elem).find('.myDate').attr('data-date-end-date', attrs.maxDate);
            }
            if (attrs.viewMode) {
                // $(elem).find('.input-group').attr('data-date-min-view-mode', attrs.viewMode);
                $(elem).find('.myDate').attr('data-date-min-view-mode', attrs.viewMode);
            }
            if (attrs.dateFormat) {
                //$(elem).find('.input-group').attr('data-date-format', attrs.dateFormat);
                $(elem).find('.myDate').attr('data-date-format', attrs.dateFormat);
                $scope.dateFormat = attrs.dateFormat;
            }
            if (attrs.ngChange) {
                $(elem).find('input').attr('ng-change', attrs.ngChange);
            }
            if (attrs.name) {
                $(elem).find('input').attr('name', attrs.name);
                if (attrs.required) {
                    $(elem).find('input').attr('required', true);
                }
                if (attrs.csValidate) {
                    $(elem).find('input').attr('cs-validate', attrs.csValidate);
                }
                if (attrs.csValidates) {
                    $(elem).find('input').attr('cs-validates', attrs.csValidates);
                }
            }
            if (!$scope.dateFormat) $scope.dateFormat = "yyyy-MM-dd";

            // var datePicker = $(elem).find('.input-group').datepicker({
            var datePicker = $(elem).find('.myDate').datepicker({
                language: 'zh-CN',
                // rtl: App.isRTL(),
                rtl: false,
                orientation: "left",
                autoclose: true,
                format: "yyyy-mm-dd",
            }).on('changeDate', function (e) {
                if (attrs.ngChange) {
                    $parse(attrs.ngChange)($scope);
                }
            });

            $scope.clear = function () {
                ctrl.$setViewValue(null);
                if (attrs.ngChange) {
                    $parse(attrs.ngChange)($scope);
                }
            }

            $scope.dateModel = function () {
                if (arguments && arguments.length > 0) {
                    if (arguments[0] == '') {

                    } else {
                        ctrl.$setViewValue(moment(arguments[0]).toDate());
                    }
                } else {
                    if (ctrl.$viewValue) {
                        if (!(ctrl.$viewValue instanceof Date)) {
                            ctrl.$setViewValue(moment(ctrl.$viewValue).toDate());
                        }
                        return $filter('date')(ctrl.$viewValue, $scope.dateFormat);
                    }
                }
            };

            $scope.disabled = function () {
                if (attrs.ngDisabled) {
                    return $parse(attrs.ngDisabled)($scope);
                } else {
                    return false;
                }
            };

            $scope.triggle = function(evnet) {
                $(elem).find('.myDate').datepicker('show');
            }

        },
        template: '<div class="input-group date date-picker" data-date-format="yyyy-mm-dd" data-date-view-mode="years" data-date-min-view-mode="days">'
                + '<input ng-disabled="disabled()" type="text" ng-model="dateModel" ng-model-options="{getterSetter: true }" class="form-control myDate" readonly >'
                + '<span class="input-group-btn">'
                + ' <button ng-disabled="disabled()" class="btn default" type="button" ng-click="clear()"><i class="fa fa-times"></i></button>'
                + ' <button ng-disabled="disabled()" class="btn default" type="button" ng-click="triggle()"><i class="fa fa-calendar"></i></button>'
                + '</span>'
                + '</div>'
    };
}]);